package com.example.config;

import com.example.mapper.SlideshowImageMapper;
import com.example.utils.MyOSSUtil;
import com.rabbitmq.client.Channel;
import org.apache.http.entity.ContentType;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author: tobed
 * @date: 2022/11/16
 * @time: 15:58
 */
@Component
public class UploadConsumer {

    @Autowired
    SlideshowImageMapper slideshowImageMapper;


    @RabbitListener(queues = UploadBean.ZJX_UPLOAD_FILE_NAME)
    public void UploadQueue(Message message, Channel channel) throws IOException {
        //接收到文件
        byte[] bytes = message.getBody();
        //字节流转化为MultiPartFile类型
        InputStream inputStream = new ByteArrayInputStream(bytes);
        MultipartFile file = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream);
        //上传OSS
        MyOSSUtil myOSSUtil = new MyOSSUtil();
        String url = myOSSUtil.uploadFile(file);
        //转化成功后插入到id
        slideshowImageMapper.createImage(url,Integer.parseInt(message.getMessageProperties().getMessageId()));
        System.out.println("----------收到信息----------");
        System.out.println("----------收到信息----------");
        System.out.println("----------收到信息----------");
    }

}
